#include "addcommoditywidget.h"
#include "ui_addcommoditywidget.h"
#include<QDebug>
#include<QDateTime>
AddCommodityWidget::AddCommodityWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddCommodityWidget)
{
    ui->setupUi(this);
    //禁止最大化
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    //固定窗口大小
    setFixedSize(width(),height());

    //光标定位(焦点)
    ui->lineEdit_comId->setFocus();


}

AddCommodityWidget::~AddCommodityWidget()
{
    delete ui;
}

void AddCommodityWidget::closeEvent(QCloseEvent *event)
{
    emit windowClosed(); // 触发关闭信号
    QWidget::closeEvent(event);
}

//点击添加按钮
void AddCommodityWidget::on_pushButton_addCom_clicked()
{
    QSqlQuery result;
    QString stock_id = ui->lineEdit_comId->text();
    QString stock_name = ui->lineEdit_comName->text();
    QString stock_amout = ui->lineEdit_comCount->text();
    QString stock_unit_price = ui->lineEdit_comUnitPrice->text();
    QString supplier = ui->lineEdit_supplier->text();
    QString director = ui->lineEdit_director->text();
    QString remarks = ui->plainTextEdit->toPlainText();
    QString store_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString delivery_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stock_classifity = ui->lineEdit_classification->text();
    QString sql = QString("insert into commodity_data"
                          " values (%1,'%2',%3,%4,'%5',"
                          "'%6','%7','%8','%9','%10')").arg(stock_id).arg(stock_name).arg(stock_amout).
            arg(stock_unit_price).arg(supplier).arg(director).arg(store_time).arg(delivery_time).arg(remarks).arg(stock_classifity);

    qDebug() << sql;
    if(stock_id.isEmpty())
    {
        QMessageBox::critical(this,"Tips","对不起，商品编号不能为空.");
        return;
    }

    if(result.exec(sql))
    {
        QMessageBox::information(this,"Tips","恭喜你，商品记录插入成功.");

        //如果插入成功就将 分类的类名 赋值给类的变量，方便交给主窗口的combox
        stockClassifiaction = stock_classifity;
    }
    else
    {
        QMessageBox::critical(this,"Tips","对不起，商品记录插入失败，请重新检查.");
        return;
    }




}

//点击取消按钮
void AddCommodityWidget::on_pushButton_cancel_clicked()
{
    this->close();
}
